package study.java.jdk.jdk8._1_stream.collectors.grouping_by;

import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import study.java.jdk.jdk8.DataUtils;
import study.java.jdk.jdk8.User;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Slf4j
public class _2_groupingBy_Test {

    private List<User> userLt = DataUtils.buildUserLt();

    /**
     * ==============================
     * （1）分组
     * （2）对分组 List 进行收集
     * ==============================
     *
     * @param classifier Function（分类器）
     * @param downstream Collector（下个收集器）
     */
    @Test
    public void test() {
        Map<String, Set<User>> dest = userLt.stream()
                .collect(Collectors.groupingBy(User::getCode,
                        Collectors.toSet()));
        log.info("{}", dest);
    }

    @Test
    public void test1() {
        Map<String, Set<String>> nameMap = userLt.stream()
                .collect(Collectors.groupingBy(User::getCode,
                        Collectors.mapping(e -> e.getName() + "===fsafsdaf", Collectors.toSet())));
        log.info("{}", nameMap);
    }

    /**
     * 统计分组的 count
     */
    @Test
    public void test2() {
        Map<String, Long> codeMap = userLt.stream()
                .collect(Collectors.groupingBy(User::getCode,
                        Collectors.counting()));
        log.info("{}", codeMap);
    }

    /**
     * 统计分组平均值
     */
    @Test
    public void test3() {
        Map<String, Double> codeMap = userLt.stream()
                .collect(Collectors.groupingBy(User::getCode,
                        Collectors.averagingDouble(User::getAge)));
        log.info("{}", codeMap);
    }

    /**
     * Join 分组 List
     */
    @Test
    public void test4() {
        Map<String, String> codeMap = userLt.stream()
                .collect(Collectors.groupingBy(User::getCode,
                        Collectors.mapping(User::getName, Collectors.joining(","))));
        log.info("{}", codeMap);
    }

    /**
     *
     */
    @Test
    public void test5() {
        Map<String, Map<String, List<User>>> codeMap = userLt.stream()
                .collect(Collectors.groupingBy(User::getCode,
                        Collectors.groupingBy(User::getName)));
        log.info("{}", codeMap);
    }

//    @Test
//    public void test6() {
//        List<QuesNumDto> dataLt = QuesNumDto.dataLt();
//        Map<Integer, List<QuesNumDto>> destLt = dataLt.stream()
//                .collect(Collectors.groupingBy(QuesNumDto::getMarkingGroup));
//        System.out.println(destLt);
//    }
}

